/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.cvut.fel.taskpro.view.controller;


import cz.cvut.fel.taskpro.business.imanager.IAssignmentBusiness;
import cz.cvut.fel.taskpro.business.imanager.IGroupBusiness;
import cz.cvut.fel.taskpro.business.imanager.ISubjectBusiness;
import cz.cvut.fel.taskpro.business.imanager.ITeamBusiness;
import cz.cvut.fel.taskpro.business.imanager.IUserBusiness;
import cz.cvut.fel.taskpro.business.support.BusinessRuleException;
import cz.cvut.fel.taskpro.data.entity.Assignment;
import cz.cvut.fel.taskpro.data.entity.AssignmentSubmit;
import cz.cvut.fel.taskpro.data.entity.GroupTaskPro;
import cz.cvut.fel.taskpro.data.entity.Student;
import cz.cvut.fel.taskpro.data.entity.Subject;
import cz.cvut.fel.taskpro.data.entity.Team;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.component.dialog.Dialog;

/**
 * This controller is responsible for working with team. It provide creating
 * team, adding team and modify team.
 *
 * @author Tom
 * @version 1.0.0. beta
 */
@ManagedBean(name = "teamBean")
@SessionScoped
public class TeamBean extends AbstractController implements Serializable {

    @EJB
    private ITeamBusiness teamBusiness;
    @EJB
    private IGroupBusiness groupBusiness;
    @EJB
    private ISubjectBusiness subjectBusiness;
    @EJB
    private IAssignmentBusiness assignmentBusiness;
    @EJB
    private IUserBusiness userB;
    private Team newTeam;
    private List<Team> teams;
    private Team selectedTeam;
    private List<Student> students;
    private Student[] selectedStudents;
    private Student[] selectedStudentsToRemove;
    private List<Subject> kantorSubject;
    private Subject subjectSelected;
    private List<GroupTaskPro> kantorGroup;
    private GroupTaskPro groupSelected;
    private boolean studentsRendered;
    private boolean groupTablerend;
    private List<Assignment> assignmentToRemove;
    private List<AssignmentSubmit> updatedAssignmentSubmit;
    private Dialog dialogWithAssginments;
    private Dialog dialogWithAssginmentsSubmit;

    public TeamBean() {
    }

    /**
     * This method initialize class.
     */
    @PostConstruct
    public void init() {
        recognizeLogin();
        resetBean();
    }

    /**
     * This method reset class so set all variables to null.
     */
    private void resetBean() {
        selectedTeam = null;
        selectedStudents = null;
        subjectSelected = null;
        groupSelected = null;
        finalStudents = null;
        studentsRendered = false;
        groupTablerend = false;
    }

    /**
     * This method prepare class to create team. It ask business layer for get
     * all subjects which kantor teach.
     *
     * @return createTeam string literar to redirect.
     * @see
     * ISubjectBusiness#getSubjectsByTeacher(cz.cvut.fel.taskpro.data.entity.Kantor)
     */
    public String teamCreation() {
        studentsRendered = false;
        groupTablerend = false;
        newTeam = new Team();
        selectedStudents = null;
        subjectSelected = null;
        groupSelected = null;
        finalStudents = null;
        try {
            kantorSubject = subjectBusiness.getSubjectsByTeacher(loggedKantor);
        } catch (BusinessRuleException ex) {
            printErrMessage(ex.getExceptionCode(), ex.getErrorMessage());
        }
        return "createTeam";
    }

    /**
     * This method ask business layer for kantor groups which are part of
     * selected subejct and are teached by logged kantor. The method also set
     * renderd view parameter.
     *
     * @see IGroupBusiness#getGroups(cz.cvut.fel.taskpro.data.entity.Subject,
     * cz.cvut.fel.taskpro.data.entity.Kantor)
     */
    public void subjectSelect() {
        groupTablerend = true;
        studentsRendered = false;
        kantorGroup = groupBusiness.getGroups(subjectSelected, loggedKantor);
        //   students = userBusiness.getStudentsNotInTeam(groupSelected);

    }

    /**
     * This method ask business layer for kantor students which are part of
     * selected group. The method also set renderd view parameter.
     *
     * @see
     * IUserBusiness#getStudentsByGroup(cz.cvut.fel.taskpro.data.entity.GroupTaskPro)
     */
    public void groupSelect() {
        try {
            students = userB.getStudentsByGroup(groupSelected);
            studentsRendered = true;
        } catch (BusinessRuleException ex) {
            printErrMessage(ex.getExceptionCode(), ex.getErrorMessage());
        }
    }

    /**
     * This method set finalStudents variable by selectedTeam.
     */
    public void selectTeam() {
        if (selectedTeam != null) {
            finalStudents = selectedTeam.getmStudents();
            studentsRendered = true;
        } else {
            printErrMessage("Select team", "No team was choosen.");
        }
    }
    /**
     * This method ask business layer for create team. If no business rule exception 
     * is invoke then return createTeam string otherwise createTeamConfirm. After 
     * creating them then reset bean.
     * @return createTeam literal if no business rule exception is invoke, otherwise
     * createTeamConfirm literal is returned.
     */
    public String createTeam() {
        newTeam.setmStudents(finalStudents);
        newTeam.setGroup(groupSelected);
        try {
            teamBusiness.addTeam(newTeam);
            printSuccMessage("Team create: ", "Team " + newTeam.getName() + " was successfully created.");
            resetBean();
            newTeam = new Team();
            return "createTeam";
        } catch (BusinessRuleException ex) {
            printErrMessage(ex.getExceptionCode(), ex.getErrorMessage());
        }
        return "createTeamConfirm";
    }   
    /**
     * This method convert selected student array to arrayList.
     * @return createTeamConfirm literal string.
     */
    public String studentSelect() {
        finalStudents = Arrays.asList(selectedStudents);
        return "createTeamConfirm";
    }
    
    public String loadTeams() {
        teams = teamBusiness.getTeamsByKantor(loggedKantor);
        return "showTeam";
    }

    public void loadStudentToAdd() {
        students = selectedTeam.getGroup().getMStudents();
    }

    public String loadTeamsByStudent() {
        finalStudents = null;
        selectedTeam = null;
        studentsRendered = false;
        teams = loggedStudent.getTeam();
        return "studentTeam";
    }

    public String loadModifactionTeams() {
        resetBean();

        teams = teamBusiness.getTeamsByKantor(loggedKantor);
        return "teamModification";
    }

    public void addStudentToTeam() {
        Team temporalyAddTeam = new Team();
        temporalyAddTeam.setName(selectedTeam.getName());
        temporalyAddTeam.setGroup(selectedTeam.getGroup());
        temporalyAddTeam.setmAssignments(null);
        temporalyAddTeam.setmStudents(Arrays.asList(selectedStudents));
        try {
            teamBusiness.addStudentToTeam(selectedTeam, temporalyAddTeam);
            printSuccMessage("Add students to team: ", "All " + selectedStudents.length + " student/s has been added to team"
                    + " " + selectedTeam.getName() + " .");
            selectedStudents = null;
        } catch (BusinessRuleException ex) {
            printErrMessage(ex.getExceptionCode(), ex.getErrorMessage());
        }
    }

    public void removeTeam() {
        try {
            teamBusiness.removeTeam(selectedTeam);
            printSuccMessage("Remove team: ", "The team: " + selectedTeam.getName() + " has been successfully removed.");
            teams.remove(selectedTeam);
            selectedTeam = null;
            students = null;
            studentsRendered = false;
            selectedStudents = null;
        } catch (BusinessRuleException ex) {
            printErrMessage(ex.getExceptionCode(), ex.getErrorMessage());
        }
    }

    public void removeAssignments() {
        try {
            assignmentBusiness.removeAssignment(assignmentToRemove);
            assignmentToRemove = null;
            printSuccMessage("Remove assignments:", "All team asssignment which has previously attached only one team were deleted.");
            closeDialog();
        } catch (BusinessRuleException ex) {
            printErrMessage(ex.getExceptionCode(), ex.getErrorMessage());
        }
    }

    public void removeStudentsFromTeam() {
        if (selectedStudentsToRemove.length == selectedTeam.getmStudents().size()) {
            removeTeam();
        } else {
            try {
                teamBusiness.removeStudentsFromTeam(selectedTeam, Arrays.asList(selectedStudentsToRemove));
                printSuccMessage("Remove students", "all students were successfully removed.");
                selectedTeam = null;
                selectedStudents = null;
                
            } catch (BusinessRuleException ex) {
                printErrMessage(ex.getExceptionCode(), ex.getErrorMessage());
            }
        }
    }

    public void closeDialog() {
        dialogWithAssginments.setVisible(false);
        dialogWithAssginmentsSubmit.setVisible(false);
    }

    public Student[] getSelectedStudentsToRemove() {
        return selectedStudentsToRemove;
    }

    public void setSelectedStudentsToRemove(Student[] selectedStudentsToRemove) {
        this.selectedStudentsToRemove = selectedStudentsToRemove;
    }

    public List<Team> getTeams() {
        return teams;
    }

    public void setTeams(List<Team> teams) {
        this.teams = teams;
    }

    public boolean isStudentsRendered() {
        return studentsRendered;
    }

    public void setStudentsRendered(boolean studentsRendered) {
        this.studentsRendered = studentsRendered;
    }

    public boolean isGroupTablerend() {
        return groupTablerend;
    }

    public void setGroupTablerend(boolean groupTablerend) {
        this.groupTablerend = groupTablerend;
    }

    public Team getNewTeam() {
        return newTeam;
    }

    public void setNewTeam(Team newTeam) {
        this.newTeam = newTeam;
    }

    public GroupTaskPro getGroupSelected() {
        return groupSelected;
    }

    public void setGroupSelected(GroupTaskPro groupSelected) {
        this.groupSelected = groupSelected;
    }

    public List<GroupTaskPro> getKantorGroup() {
        return kantorGroup;
    }

    public void setKantorGroup(List<GroupTaskPro> kantorGroup) {
        this.kantorGroup = kantorGroup;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public List<Subject> getKantorSubject() {
        return kantorSubject;
    }

    public void setKantorSubject(List<Subject> kantorSubject) {
        this.kantorSubject = kantorSubject;
    }

    public Student[] getSelectedStudents() {
        return selectedStudents;
    }

    public void setSelectedStudents(Student[] selectedStudents) {
        this.selectedStudents = selectedStudents;
    }

    public Team getSelectedTeam() {
        return selectedTeam;
    }

    public void setSelectedTeam(Team selectedTeam) {
        this.selectedTeam = selectedTeam;
    }

    public List<Student> getFinalStudents() {
        return finalStudents;
    }

    public void setFinalStudents(List<Student> finalStudents) {
        this.finalStudents = finalStudents;
    }
    private List<Student> finalStudents;

    public Subject getSubjectSelected() {
        return subjectSelected;
    }

    public void setSubjectSelected(Subject subjectSelected) {
        this.subjectSelected = subjectSelected;
    }

    public List<Assignment> getAssignmentToRemove() {
        return assignmentToRemove;
    }

    public void setAssignmentToRemove(List<Assignment> assignmentToRemove) {
        this.assignmentToRemove = assignmentToRemove;
    }

    public Dialog getDialogWithAssginments() {
        return dialogWithAssginments;
    }

    public void setDialogWithAssginments(Dialog dialogWithAssginments) {
        this.dialogWithAssginments = dialogWithAssginments;
    }

    public List<AssignmentSubmit> getUpdatedAssignmentSubmit() {
        return updatedAssignmentSubmit;
    }

    public void setUpdatedAssignmentSubmit(List<AssignmentSubmit> updatedAssignmentSubmit) {
        this.updatedAssignmentSubmit = updatedAssignmentSubmit;
    }

    public Dialog getDialogWithAssginmentsSubmit() {
        return dialogWithAssginmentsSubmit;
    }

    public void setDialogWithAssginmentsSubmit(Dialog dialogWithAssginmentsSubmit) {
        this.dialogWithAssginmentsSubmit = dialogWithAssginmentsSubmit;
    }
}
